<template>
  <div class="profile">
    <!-- 加载状态 -->
    <div v-if="loading" class="loading-container">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <!-- 用户信息头部 -->
    <div v-else class="profile-header">
      <div class="avatar-section">
        <div class="avatar-container">
          <img :src="userInfo.avatar || defaultAvatar" :alt="userInfo.nickname" class="avatar" />
          <div class="avatar-badge">
            <i class="icon-verified"></i>
          </div>
        </div>
        <div class="user-info">
          <h2 class="nickname">{{ userInfo.nickname }}</h2>
          <p class="user-id">ID: {{ userInfo.id }}</p>
          <p class="join-date">邮箱: {{ userInfo.email }}</p>
        </div>
      </div>

      <!-- 统计数据 -->
      <div class="stats-section">
        <div class="stat-item">
          <span class="stat-number">{{ userInfo.postsCount }}</span>
          <span class="stat-label">发帖</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ userInfo.commentsCount }}</span>
          <span class="stat-label">评论</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ userInfo.fansCount }}</span>
          <span class="stat-label">粉丝</span>
        </div>
        <div class="stat-item">
          <span class="stat-number">{{ userInfo.followingCount }}</span>
          <span class="stat-label">关注</span>
        </div>
      </div>
    </div>

    <!-- 功能菜单 -->
    <div v-if="!loading" class="menu-section">
      <div class="menu-group">
        <div class="menu-item" @click="navigateTo('/user/my-posts')">
          <div class="menu-icon posts">
            <i class="icon-posts"></i>
          </div>
          <span class="menu-text">我的发帖</span>
          <i class="icon-arrow-right"></i>
        </div>

        <div class="menu-item" @click="navigateTo('/user/my-comments')">
          <div class="menu-icon comments">
            <i class="icon-comments"></i>
          </div>
          <span class="menu-text">我的评论</span>
          <i class="icon-arrow-right"></i>
        </div>

        <div class="menu-item" @click="showNotDeveloped('粉丝')">
          <div class="menu-icon fans">
            <i class="icon-fans"></i>
          </div>
          <span class="menu-text">粉丝</span>
          <span class="menu-badge">{{ userInfo.fansCount }}</span>
          <i class="icon-arrow-right"></i>
        </div>

        <div class="menu-item" @click="showNotDeveloped('私信')">
          <div class="menu-icon messages">
            <i class="icon-messages"></i>
          </div>
          <span class="menu-text">私信</span>
          <span class="menu-badge unread" v-if="userInfo.unreadMessages > 0">{{ userInfo.unreadMessages }}</span>
          <i class="icon-arrow-right"></i>
        </div>
      </div>

      <div class="menu-group">
        <div class="menu-item" @click="navigateTo('/user/settings')">
          <div class="menu-icon settings">
            <i class="icon-settings"></i>
          </div>
          <span class="menu-text">设置</span>
          <i class="icon-arrow-right"></i>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { getUserInfoById, getUserCenterCount } from '@/api/user'
import defaultAvatar from '@/assets/touxiang.png'

const router = useRouter()
const userStore = useUserStore()

// 用户信息数据
const userInfo = ref({
  id: '',
  nickname: '',
  avatar: '',
  email: '',
  mobile: '',
  sex: 0,
  status: 0,
  joinDate: '',
  postsCount: 0,
  commentsCount: 0,
  fansCount: 0,
  followingCount: 0,
  unreadMessages: 0
})

// 加载状态
const loading = ref(true)

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return '未知'
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN', {
    year: 'numeric',
    month: 'long',
    day: 'numeric'
  })
}

// 导航到指定页面
const navigateTo = (path) => {
  router.push(path)
}

// 显示暂未开发提示
const showNotDeveloped = (feature) => {
  alert(`${feature}功能暂未开发，敬请期待！`)
}

// 获取用户信息
const fetchUserInfo = async () => {
  // 检查用户是否已登录
  if (!userStore.uid) {
    console.error('用户未登录')
    router.push('/user/login')
    return
  }

  loading.value = true
  try {
    const res = await getUserInfoById(userStore.uid)
    if (res.code === 200) {
      // 将API返回的数据映射到组件的数据结构
      const apiData = res.data
      userInfo.value = {
        id: apiData.id,
        nickname: apiData.nickName || '未设置昵称',
        avatar: apiData.avatar || defaultAvatar,
        email: apiData.email || '',
        mobile: apiData.mobile || '',
        sex: apiData.sex || 0,
        status: apiData.status || 0,
        joinDate: '', // API没有返回加入日期，使用空字符串
        postsCount: 0, // 这些数据需要额外的API调用
        commentsCount: 0,
        fansCount: 0,
        followingCount: 0,
        unreadMessages: 0
      }

      // 更新用户存储中的信息
      userStore.setUserInfo(apiData)
      
      // 获取用户统计数据
      await fetchUserStats()
    } else {
      console.error('获取用户信息失败:', res.msg || '未知错误')
    }
  } catch (error) {
    console.error('获取用户信息失败:', error)
  } finally {
    loading.value = false
  }
}

// 获取用户统计数据
const fetchUserStats = async () => {
  try {
    const res = await getUserCenterCount()
    if (res.code === 200) {
      // 更新统计数据
      userInfo.value.postsCount = res.data.forumPostCount || 0
      userInfo.value.commentsCount = res.data.commentCount || 0
      userInfo.value.fansCount = res.data.fansCount || 0
      userInfo.value.followingCount = res.data.followCount || 0
    } else {
      console.error('获取用户统计数据失败:', res.msg || '未知错误')
    }
  } catch (error) {
    console.error('获取用户统计数据失败:', error)
  }
}

// 页面加载时获取用户信息
onMounted(() => {
  fetchUserInfo()
})
</script>

<style scoped>
/* 加载状态样式 */
.loading-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 300px;
  color: #666;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #3498db;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

.profile {
  min-height: 100vh;
  background: #ffffff;
  padding: 0;
}

/* 用户信息头部 */
.profile-header {
  background: linear-gradient(135deg, #1976d2 0%, #42a5f5 50%, #81d4fa 100%);
  padding: 20px;
  color: white;
  position: relative;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(25, 118, 210, 0.3);
}

.profile-header::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.15) 0%, transparent 70%);
  animation: techFloat 8s ease-in-out infinite;
}

.profile-header::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="60" height="60" viewBox="0 0 60 60"><defs><pattern id="grid" width="60" height="60" patternUnits="userSpaceOnUse"><path d="M 60 0 L 0 0 0 60" fill="none" stroke="rgba(255,255,255,0.1)" stroke-width="1"/></pattern></defs><rect width="100%" height="100%" fill="url(%23grid)"/></svg>');
  opacity: 0.3;
}

@keyframes techFloat {

  0%,
  100% {
    transform: translateY(0px) rotate(0deg) scale(1);
  }

  33% {
    transform: translateY(-15px) rotate(120deg) scale(1.05);
  }

  66% {
    transform: translateY(-10px) rotate(240deg) scale(0.95);
  }
}

.avatar-section {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  position: relative;
  z-index: 2;
}

.avatar-container {
  position: relative;
  margin-right: 15px;
}

.avatar {
  width: 80px;
  height: 80px;
  border-radius: 16px;
  border: 3px solid rgba(255, 255, 255, 0.4);
  object-fit: cover;
  box-shadow: 0 12px 24px rgba(25, 118, 210, 0.3);
  transition: all 0.3s ease;
}

.avatar:hover {
  transform: scale(1.05);
  box-shadow: 0 16px 32px rgba(25, 118, 210, 0.4);
}

.avatar-badge {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 24px;
  height: 24px;
  background: linear-gradient(135deg, #00e676, #00c853);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 3px solid white;
  box-shadow: 0 4px 12px rgba(0, 230, 118, 0.4);
}

.user-info {
  flex: 1;
}

.nickname {
  font-size: 1.5rem;
  font-weight: 700;
  margin: 0 0 5px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.user-id,
.join-date {
  margin: 2px 0;
  opacity: 0.9;
  font-size: 0.9rem;
}

/* 统计数据 */
.stats-section {
  display: flex;
  justify-content: space-around;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 16px;
  padding: 16px;
  backdrop-filter: blur(15px);
  position: relative;
  z-index: 2;
  border: 1px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 8px 24px rgba(25, 118, 210, 0.2);
}

.stat-item {
  text-align: center;
  flex: 1;
}

.stat-number {
  display: block;
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 0.8rem;
  opacity: 0.9;
}

/* 功能菜单 */
.menu-section {
  background: #ffffff;
  flex: 1;
  padding: 20px;
  border-radius: 28px 28px 0 0;
  margin-top: -18px;
  position: relative;
  z-index: 3;
  box-shadow: 0 -4px 20px rgba(25, 118, 210, 0.1);
}

.menu-group {
  background: white;
  border-radius: 18px;
  margin-bottom: 16px;
  box-shadow: 0 6px 20px rgba(25, 118, 210, 0.08);
  overflow: hidden;
  border: 1px solid rgba(25, 118, 210, 0.05);
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 18px 20px;
  border-bottom: 1px solid #f1f5f9;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-item:hover {
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  transform: translateX(6px);
}

.menu-item:active {
  background: linear-gradient(135deg, #e1f5fe 0%, #b3e5fc 100%);
  transform: scale(0.98);
}

.menu-icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
  font-size: 1.2rem;
  color: white;
}

.menu-icon.posts {
  background: linear-gradient(135deg, #1976d2, #1565c0);
  box-shadow: 0 4px 12px rgba(25, 118, 210, 0.3);
}

.menu-icon.comments {
  background: linear-gradient(135deg, #0288d1, #0277bd);
  box-shadow: 0 4px 12px rgba(2, 136, 209, 0.3);
}

.menu-icon.fans {
  background: linear-gradient(135deg, #039be5, #0288d1);
  box-shadow: 0 4px 12px rgba(3, 155, 229, 0.3);
}

.menu-icon.messages {
  background: linear-gradient(135deg, #00acc1, #0097a7);
  box-shadow: 0 4px 12px rgba(0, 172, 193, 0.3);
}

.menu-icon.settings {
  background: linear-gradient(135deg, #546e7a, #455a64);
  box-shadow: 0 4px 12px rgba(84, 110, 122, 0.3);
}

.menu-text {
  flex: 1;
  font-size: 1rem;
  font-weight: 500;
  color: #1f2937;
}

.menu-badge {
  background: #e5e7eb;
  color: #6b7280;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.8rem;
  margin-right: 10px;
}

.menu-badge.unread {
  background: linear-gradient(135deg, #ff5722, #f44336);
  color: white;
  animation: techPulse 2s infinite;
  box-shadow: 0 2px 8px rgba(255, 87, 34, 0.4);
}

@keyframes techPulse {

  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }

  50% {
    opacity: 0.8;
    transform: scale(1.05);
  }
}

.icon-arrow-right {
  color: #9ca3af;
  font-size: 0.9rem;
}

/* 图标样式 */
.icon-verified::before {
  content: '✓';
}

.icon-posts::before {
  content: '📝';
}

.icon-comments::before {
  content: '💬';
}

.icon-fans::before {
  content: '👥';
}

.icon-messages::before {
  content: '✉️';
}

.icon-settings::before {
  content: '⚙️';
}

.icon-arrow-right::before {
  content: '›';
}

/* 移动端适配 */
@media (max-width: 768px) {
  .profile-header {
    padding: 15px;
  }

  .avatar {
    width: 70px;
    height: 70px;
    border-radius: 14px;
  }

  .nickname {
    font-size: 1.3rem;
  }

  .stats-section {
    padding: 12px;
  }

  .stat-number {
    font-size: 1.2rem;
  }

  .menu-section {
    padding: 15px;
  }

  .menu-item {
    padding: 15px 18px;
  }

  .menu-icon {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }
}

/* 超小屏幕适配 */
@media (max-width: 480px) {
  .profile-header {
    padding: 12px;
  }

  .avatar {
    width: 60px;
    height: 60px;
    border-radius: 12px;
  }

  .avatar-container {
    margin-right: 12px;
  }

  .nickname {
    font-size: 1.2rem;
  }

  .user-id,
  .join-date {
    font-size: 0.8rem;
  }

  .stats-section {
    padding: 10px;
  }

  .stat-number {
    font-size: 1.1rem;
  }

  .stat-label {
    font-size: 0.75rem;
  }

  .menu-section {
    padding: 12px;
  }

  .menu-item {
    padding: 12px 15px;
  }

  .menu-icon {
    width: 32px;
    height: 32px;
    margin-right: 12px;
    font-size: 1rem;
  }

  .menu-text {
    font-size: 0.9rem;
  }
}

/* 横屏适配 */
@media (max-height: 600px) and (orientation: landscape) {
  .profile-header {
    padding: 10px 15px;
  }

  .avatar-section {
    margin-bottom: 10px;
  }

  .stats-section {
    padding: 8px;
  }

  .menu-section {
    padding: 10px 15px;
  }

  .menu-item {
    padding: 10px 15px;
  }
}
</style>